﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeadVinaglue.Models;
using LeadVinaglue.Models.ViewPages;
using LeadVinaglue.Repository;
using LeadVinaglue.Utility;
using System.Configuration;

namespace LeadVinaglue.Controllers
{
    public class RaovatController : Controller
    {
        LEManagementRepository repository = new LEManagementRepository();

        // Find
        [DeviceFilterAttribute(Order = 1)]
        [CompressFilter(Order = 2)]
        public ActionResult FindProduct(string q, int? cat, int? page)
        {
            int catId = (cat == null) ? 0 : Convert.ToInt32(cat.ToString());
            int pageIndex = (page == null) ? 1 : Convert.ToInt32(page.ToString()), pageSize = 12, count = 0;
            string url = string.Format("/tim-kiem?q={0}&cat={1}", q, catId) + "&page={0}";

            LEManagementModel model = new LEManagementModel();

            q = q.Replace("+", " ");
            model = repository.GetModelByKeywords(LeadConstant.SEARCH, q, url, catId, 0, pageIndex, pageSize, ref count);
            model.Keyword = q;
            model.CategoryId = catId;

            return View(model);
        }


        // GET: /Raovat/
        [DeviceFilterAttribute(Order = 1)]
        [CompressFilter(Order = 2)]
        public ActionResult CategoryList(int categoryId, int? subId, string name, int? page)
        {
            int subValue = (subId == null) ? 0 : subId.Value;
            int pageIndex = (page == null) ? 1 : Convert.ToInt32(page.ToString()), pageSize = 24, count = 0;
            string url = Request.RawUrl.ToLower();
            if (url.IndexOf("?page=") > -1)
                url = url.Substring(0, url.IndexOf("?page="));
            url = url + "?page={0}";

            LEManagementModel model = new LEManagementModel();
            model = repository.GetModelByCategory(LeadConstant.CATEGORY, url, categoryId, subValue, pageIndex, pageSize, ref count);

            return View(model);
        }

        [DeviceFilterAttribute(Order = 1)]
        [CompressFilter(Order = 2)]
        public ActionResult AllCategories()
        {

            LEManagementModel model = new LEManagementModel();
            model = repository.GetAllCategories(LeadConstant.CATEGORY);

            return View(model);
        }

        [DeviceFilterAttribute(Order = 1)]
        [CompressFilter(Order = 2)]
        public ActionResult AllStates()
        {

            LEManagementModel model = new LEManagementModel();
            model = repository.GetAllStates(LeadConstant.ZONE);

            return View(model);
        }

        [DeviceFilterAttribute(Order = 1)]
        [CompressFilter(Order = 2)]
        [CacheFilter(Duration = 3600, Order = 3)]
        public ActionResult DacBiet(int? page)
        {
            int pageIndex = (page == null) ? 1 : Convert.ToInt32(page.ToString()), pageSize = 24, count = 0;
            string url = Request.RawUrl.ToLower();

            if (url.IndexOf("?page=") > -1)
                url = url.Substring(0, url.IndexOf("?page="));
            url = url + "?page={0}";

            LEManagementModel model = new LEManagementModel();
            model = repository.GetModelBySpecials(LeadConstant.PRODUCTSPECIAL, url, pageIndex, pageSize, ref count);

            return View(model);
        }

        // Product Details
        [DeviceFilterAttribute(Order = 1)]
        [CompressFilter(Order = 2)]
        public ActionResult Detail(int id, string name)
        {
            LEManagementModel model = new LEManagementModel();
            model = repository.GetProductModel(LeadConstant.PRODUCTINFO, id);

            if (model != null && model.ProductInfo != null)
            {
                model.SharePageTitle = string.Format("{0} - Rao vặt toàn quốc - Raovat24.net", model.ProductInfo.Title);
                model.ShareTwitterTitle = string.Format("{0}", model.ProductInfo.Title);
                if (!string.IsNullOrEmpty(model.ProductInfo.ImageURL))
                {
                    model.SharePageImageUrl = string.Format("{0}{1}/{2}", ConfigurationManager.AppSettings["WebsiteURL"].ToString(), ConfigurationManager.AppSettings["ProductShareImagesURL"].ToString(), model.ProductInfo.ImageURL);
                }
                else
                {
                    model.SharePageImageUrl = string.Format("{0}/images/noimagelarge.jpg", ConfigurationManager.AppSettings["WebsiteURL"].ToString());
                }
            }
            model.SharePageUrl = string.Format("{0}{1}", ConfigurationManager.AppSettings["WebsiteURL"].ToString(), Request.RawUrl.ToLower());
            model.ShareTwitterUrl = model.SharePageUrl;

            if (model.ShareTwitterTitle.Length > 70)
            {
                model.ShareTwitterTitle = StringClearFormat.Clear75(model.ShareTwitterTitle).Replace("|", string.Empty).Replace("<", string.Empty).Replace(">", string.Empty) + "...";
                model.ShareTwitterUrl = string.Format("{0}/raovat/d{1}/{2}", ConfigurationManager.AppSettings["WebsiteURL"].ToString(), id, StringClearFormat.ClearSpecials(model.ShareTwitterTitle));
            }

            return View(model);
        }
    }
}
